package autoz.core;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.uml2.uml.UMLPackage;
import org.osgi.framework.BundleContext;

import autoz.core.UserNotification.MessageType;

/**
 * The activator class controls the plug-in life cycle
 */
public class AutozCorePlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "autoz.core";

	// The shared instance
	private static AutozCorePlugin plugin;
	
	/**
	 * The constructor
	 */
	public AutozCorePlugin() {
		super();
		
		// Register UML2 ecore package, just in case it hasn't been detected.
		EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static AutozCorePlugin getDefault() {
		return plugin;
	}
	/**
	 * Logs messages to the Eclipse error log
	 * @param errorStatus IStatus error messages
	 * @param message
	 */
	public static void logMessage(int errorStatus, String message, Throwable stackTrace, Boolean throwDialog){
		IStatus status = new Status(errorStatus, PLUGIN_ID, message, stackTrace);
		getDefault().getLog().log(status);
		
		message += "\n\nSee error log for more details.";
		
		if(throwDialog){
			displayUserNotification(message, MessageType.ERROR);
		}
	}
	
	public static void displayUserNotification(String message, MessageType type){
		Display.getDefault().syncExec(new UserNotification(message, type));
	}
}
